package com.ny.view;

import com.ny.entity.Manager;
import com.ny.entity.Order;
import com.ny.entity.Product;
import com.ny.entity.Student;
import com.ny.service.ManagerService;
import com.ny.service.OrderService;
import com.ny.service.ProductService;
import com.ny.service.StudentService;
import com.ny.service.impl.ManagerServiceImpl;
import com.ny.service.impl.OrderServiceImpl;
import com.ny.service.impl.ProductServiceImpl;
import com.ny.service.impl.StudentServiceImpl;

import java.util.List;
import java.util.Scanner;

public class IndexView {
    private static StudentService studentService = new StudentServiceImpl();
    private static ProductService productService = new ProductServiceImpl();
    private static OrderService orderService = new OrderServiceImpl();
    private static ManagerService managerService = new ManagerServiceImpl();
    public static void main(String[] args) throws Exception{

        boolean flag = true;//循环状态额判断
        while (flag) {
            System.out.println("欢迎来到校园商品系统");
            System.out.println("请选择");
            System.out.println("1.用户登录");
            System.out.println("2.用户注册");
            System.out.println("3.修改个人信息");
            System.out.println("4.进入管理员系统");
            System.out.println("5.退出系统");
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            switch (n) {
                case 1:
                    System.out.println("请登录");
                    System.out.println("请输入用户名");
                    String username = scanner.next();
                    System.out.println("请输入密码");
                    String password = scanner.next();
                    //判断输入的用户名和密码和数据库里的用户名和密码是否一致
                    Student student = studentService.login(username, password);
                    if (student != null) {
                        System.out.println("登录成功：来到商品显示界面");
                        List<Product> productList = productService.findAllProduct();
                        System.out.print("商品序号"+"\t"+"\t");
                        System.out.print("商品名称"+"\t"+"\t");
                        System.out.print("商品库存"+"\t"+"\t");
                        System.out.print("商品单价"+"\t"+"\t");
                        System.out.print("商品种类"+"\t"+"\t");
                        System.out.println("商品介绍"+"\t"+"\t");
                        for (Product product : productList){
                            System.out.print(product.getProductId()+" "+" "+"\t"+"\t"+"\t");
                            System.out.print(product.getName()+" "+" "+"\t"+"\t");
                            System.out.print(product.getStock()+"\t"+"\t"+"\t");
                            System.out.print(product.getPrice()+" "+" "+"\t"+"\t");
                            System.out.print(product.getCategory()+" "+" "+"\t"+"\t");
                            System.out.println(product.getDescription()+" "+" "+"\t"+"\t");
                        }

                        System.out.println("请输入您想购买的商品编号：");
                        int productId = scanner.nextInt();
                        System.out.println("请输入购买数量：");
                        int numberStr = scanner.nextInt();

                        Order order = new Order();
                        order.setStudentId(student.getStudentId());
                        order.setProductId(productId);
                        order.setNumber(numberStr);
                        order.setPrice(productList.get(productId-1).getPrice()*numberStr);

                        int i =  orderService.insertOrder(order);
                        if(i>0){
                            System.out.println("购买成功");
                        }else{
                            System.out.println("购买失败");
                        }

                    } else {
                        System.out.println("重新登录");
                    }


                    break;
                case 2:
                    System.out.println("请注册");
                    System.out.println("请输入用户名");
                    String name = scanner.next();
                    System.out.println("请输入手机号");
                    String phone = scanner.next();
                    System.out.println("请输入密码");
                    String pwd = scanner.next();

                    Student s = new Student();
                    s.setName(name);
                    s.setPhone(phone);
                    s.setPassword(pwd);

                    int i = studentService.insertStudent(s);
                    if (i > 0) {
                        System.out.println("注册成功");
                    } else {
                        System.out.println("注册失败");
                    }

                    break;
                case 3:
                    System.out.println("请输入要修改的用户名");
                    String oldname = scanner.next();
                    System.out.println("请输入需要修改的用户名的密码");
                    String oldpwd = scanner.next();

                    Student Ups = studentService.login(oldname, oldpwd);
                    if (Ups == null) {
                        System.out.println("用户不存在");
                        break;
                    }

                    System.out.println("当前信息：");
                    System.out.println("用户名：" + Ups.getName());
                    System.out.println("手机号：" + Ups.getPhone());

                    System.out.println("请输入新的用户名");
                    String newname = scanner.next();
                    System.out.println("请输入新的手机号");
                    String newphone = scanner.next();
                    System.out.println("请输入新的密码");
                    String newpwd = scanner.next();

                    Ups.setName(newname);
                    Ups.setPhone(newphone);
                    Ups.setPassword(newpwd);

                    int u = studentService.updateStudent(Ups);
                    if (u > 0) {
                        System.out.println("修改成功");
                    } else {
                        System.out.println("修改失败");
                    }

                    break;

                case 4:
                    boolean flag1 = true;
                    while (flag1) {
                        System.out.println("欢迎来到管理员系统");
                        System.out.println("请选择");
                        System.out.println("1.管理用户信息");
                        System.out.println("2.管理商品信息");
                        System.out.println("3.管理订单信息");

                        int d = scanner.nextInt();
                        switch (d) {
                            case 1:
                                boolean flag2 = true;
                                while (flag2) {
                                    System.out.println("1.管理员用户登录");
                                    System.out.println("2.管理员注册");
                                    System.out.println("3.管理员信息修改");
                                    System.out.println("4.管理员用户删除");
                                    System.out.println("5.退出管理员系统");

                                    int a = scanner.nextInt();
                                    switch (a) {
                                        case 1:
                                            System.out.println("请登录");
                                            System.out.println("请输入管理员用户名");
                                            String Managername = scanner.next();
                                            System.out.println("请输入管理员密码");
                                            String Managerpwd = scanner.next();

                                            Manager manager = managerService.login(Managername, Managerpwd);
                                            if (manager != null) {
                                                System.out.println("登录成功：来到管理员信息显示界面");
                                                List<Manager> managerList = managerService.findAllManager();
                                                System.out.print("管理员编号" + "\t" + "\t");
                                                System.out.print("管理员姓名" + "\t" + "\t");
                                                System.out.print("管理员电话" + "\t" + "\t");
                                                System.out.println("管理员密码");

                                                for (Manager manager1 : managerList) {
                                                    System.out.print(manager1.getManagerId() + "\t" + "\t" + " " + " " + " " + " " + " " + " " + " " + " ");
                                                    System.out.print(manager1.getName() + "\t" + "\t" + " " + " " + " ");
                                                    System.out.print(manager1.getPhone() + " " + "\t" + "\t");
                                                    System.out.println(manager1.getPassword() + " " + "\t" + "\t");
                                                }
                                            }
                                            break;

                                        case 2:
                                            System.out.println("请注册");
                                            System.out.println("请输入用户名");
                                            String ManagerName = scanner.next();
                                            System.out.println("请输入手机号");
                                            String ManagerPhone = scanner.next();
                                            System.out.println("请输入密码");
                                            String ManagerPassword = scanner.next();

                                            Manager m = new Manager();
                                            m.setName(ManagerName);
                                            m.setPhone(ManagerPhone);
                                            m.setPassword(ManagerPassword);

                                            int b = managerService.insertManager(m);
                                            if (b > 0) {
                                                System.out.println("注册成功");
                                            } else {
                                                System.out.println("注册失败");
                                            }
                                            break;

                                        case 3:
                                            System.out.println("请输入要修改的用户名");
                                            String oldMname = scanner.next();
                                            System.out.println("请输入需要修改的用户名的密码");
                                            String oldMpwd = scanner.next();

                                            Manager Upm = managerService.login(oldMname, oldMpwd);
                                            if (Upm == null) {
                                                System.out.println("用户不存在");
                                                break;
                                            }

                                            System.out.println("当前信息：");
                                            System.out.println("用户名：" + Upm.getName());
                                            System.out.println("手机号：" + Upm.getPhone());

                                            System.out.println("请输入新的用户名");
                                            String newMname = scanner.next();
                                            System.out.println("请输入新的手机号");
                                            String newpMhone = scanner.next();
                                            System.out.println("请输入新的密码");
                                            String newMpwd = scanner.next();

                                            Upm.setName(newMname);
                                            Upm.setPhone(newpMhone);
                                            Upm.setPassword(newMpwd);

                                            int c = managerService.updateManager(Upm);
                                            if (c > 0) {
                                                System.out.println("修改成功");
                                            } else {
                                                System.out.println("修改失败");
                                            }
                                            break;

                                        case 4:
                                            System.out.println("请输入要删除的管理员姓名：");
                                            String Dname = scanner.next();
                                            int e = managerService.deleteManager(Dname);
                                            if (d > 0) {
                                                System.out.println("删除成功");
                                            } else {
                                                System.out.println("删除失败");
                                            }
                                            break;


                                    }

                                }
                            case 2:
                             {
                                 boolean productMenu = true;
                                 while (productMenu) {
                                    System.out.println("====== 商品管理 ======");
                                    System.out.println("1. 查看所有商品");
                                    System.out.println("2. 新增商品");
                                    System.out.println("3. 修改商品");
                                    System.out.println("4. 删除商品");
                                    System.out.println("5. 返回上一级");
                                    int choice = scanner.nextInt();

                                    switch (choice) {
                                        case 1:
                                            // 查看所有商品
                                            try {
                                                List<Product> productList = productService.findAllProduct();
                                                System.out.print("ID\t");
                                                System.out.print("名称\t");
                                                System.out.print("价格\t");
                                                System.out.print("库存\t");
                                                System.out.print("类别\t");
                                                System.out.println("描述");

                                                for (Product product : productList) {
                                                    System.out.println(product.getProductId() + "\t" +
                                                            product.getName() + "\t" +
                                                            product.getPrice() + "\t" +
                                                            product.getStock() + "\t" +
                                                            product.getCategory() + "\t" +
                                                            product.getDescription());
                                                }
                                            } catch (Exception e) {
                                                System.out.println("获取商品列表失败");
                                                e.printStackTrace();
                                            }
                                            break;

                                        case 5:
                                            // 返回上级菜单
                                            productMenu = false;
                                            break;

                                        default:
                                            System.out.println("无效选择，请重新输入！");
                                    }
                                }
                            }


                        }




                    }


                case 5:
                    System.out.println("退出系统");
                    flag = false;
                    break;
                default:
                    System.out.println("请正确输出选择序号");
            }
        }
    }

    private static void manageProducts(Scanner scanner) {
    }
}
